<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>角色管理 - 管理员控制台</title>
    <link href="https://cdn.jsdelivr.net/npm/remixicon@3.5.0/fonts/remixicon.css" rel="stylesheet">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
            background: #f5f7fa;
            min-height: 100vh;
        }
        .top-nav {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 0 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 1000;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .logo {
            font-size: 24px;
            font-weight: bold;
            display: flex;
            align-items: center;
            gap: 10px;
            text-decoration: none;
            color: white;
        }
        .logout-btn {
            padding: 8px 20px;
            background: rgba(255,255,255,0.2);
            border: 1px solid rgba(255,255,255,0.3);
            color: white;
            border-radius: 20px;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 5px;
        }
        .container { margin-top: 80px; padding: 30px; }
        .page-header {
            margin-bottom: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .page-title { font-size: 28px; color: #333; }
        .btn-primary {
            padding: 10px 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .alert {
            padding: 12px 20px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        .alert-success { background: #d1fae5; border: 1px solid #10b981; color: #059669; }
        .alert-error { background: #fee2e2; border: 1px solid #ef4444; color: #dc2626; }
        .roles-table-container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            overflow: hidden;
        }
        .table-header { padding: 20px; border-bottom: 1px solid #e0e0e0; }
        table { width: 100%; border-collapse: collapse; }
        thead { background: #f8f9fa; }
        th { padding: 15px; text-align: left; font-weight: 600; color: #666; font-size: 14px; }
        td { padding: 15px; border-top: 1px solid #f0f0f0; font-size: 14px; }
        tbody tr:hover { background: #f8f9fa; }
        .role-badge { padding: 4px 12px; border-radius: 12px; font-size: 12px; margin-right: 5px; }
        .role-admin { background: #fee2e2; color: #dc2626; }
        .role-teacher { background: #dbeafe; color: #2563eb; }
        .role-student { background: #d1fae5; color: #059669; }
        .role-custom { background: #fef3c7; color: #d97706; }
        .perm-badge { 
            display: inline-block;
            padding: 3px 10px; 
            border-radius: 10px; 
            font-size: 11px; 
            margin: 2px;
            background: #e0e7ff;
            color: #4338ca;
        }
        .btn-small {
            padding: 6px 12px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 12px;
            margin-right: 5px;
        }
        .btn-edit { background: #dbeafe; color: #2563eb; }
        .btn-delete { background: #fee2e2; color: #dc2626; }
        .btn-manage { background: #d1fae5; color: #059669; }
        
        /* 模态框 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 2000;
            align-items: center;
            justify-content: center;
        }
        .modal.show { display: flex; }
        .modal-content {
            background: white;
            border-radius: 12px;
            padding: 30px;
            max-width: 600px;
            width: 90%;
            max-height: 80vh;
            overflow-y: auto;
        }
        .modal-header { font-size: 20px; font-weight: 600; margin-bottom: 20px; color: #333; }
        .form-group { margin-bottom: 15px; }
        .form-group label { display: block; margin-bottom: 5px; font-size: 14px; color: #666; }
        .form-group input, .form-group select {
            width: 100%;
            padding: 10px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            font-size: 14px;
        }
        .modal-actions {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
            margin-top: 20px;
        }
        .btn-cancel { padding: 10px 20px; background: #f0f0f0; color: #666; border: none; border-radius: 8px; cursor: pointer; }
        .btn-confirm { padding: 10px 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border: none; border-radius: 8px; cursor: pointer; }
        
        .permissions-list {
            margin: 20px 0;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 8px;
            max-height: 300px;
            overflow-y: auto;
        }
        .perm-item {
            display: inline-flex;
            align-items: center;
            padding: 5px 12px;
            margin: 5px;
            background: #e0e7ff;
            color: #4338ca;
            border-radius: 15px;
            font-size: 12px;
        }
        .perm-item i {
            margin-left: 8px;
            cursor: pointer;
        }
        .perm-item i:hover {
            color: #dc2626;
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <div class="top-nav">
        <a href="/admin/index" class="logo">
            <i class="ri-admin-line"></i>
            <span>管理员控制台</span>
        </a>
        <a href="/perform_logout" class="logout-btn">
            <i class="ri-logout-box-line"></i> 退出登录
        </a>
    </div>

    <!-- 主容器 -->
    <div class="container">
        <div class="page-header">
            <h1 class="page-title">角色管理</h1>
            <button class="btn-primary" onclick="showAddRoleModal()">
                <i class="ri-user-settings-line"></i> 添加角色
            </button>
        </div>

        <!-- 提示消息 -->
        <div class="alert alert-success" th:if="${success}" th:text="${success}"></div>
        <div class="alert alert-error" th:if="${error}" th:text="${error}"></div>

        <!-- 角色列表 -->
        <div class="roles-table-container">
            <div class="table-header">
                <h3>角色列表（共 <span th:text="${roles.size()}">0</span> 个）</h3>
            </div>

            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>角色代码</th>
                        <th>角色名称</th>
                        <th>拥有权限</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="role : ${roles}">
                        <td th:text="${role.id}">1</td>
                        <td>
                            <span th:text="${role.roleCode}" 
                                  th:class="${role.roleCode == '0' ? 'role-badge role-admin' : 
                                           (role.roleCode == '1' ? 'role-badge role-teacher' : 
                                           (role.roleCode == '2' ? 'role-badge role-student' : 'role-badge role-custom'))}">
                                代码
                            </span>
                        </td>
                        <td th:text="${role.roleName}">角色名</td>
                        <td>
                            <th:block th:each="perm : ${role.permissions}">
                                <span class="perm-badge" th:text="${perm.permName}">权限</span>
                            </th:block>
                            <span th:if="${#lists.isEmpty(role.permissions)}" style="color: #999;">暂无权限</span>
                        </td>
                        <td>
                            <button class="btn-small btn-manage" 
                                    th:attr="data-id=${role.id}, data-rolename=${role.roleName}"
                                    onclick="manageRolePermissions(this)">
                                <i class="ri-key-line"></i> 管理权限
                            </button>
                            <button class="btn-small btn-edit" 
                                    th:attr="data-id=${role.id}, data-rolecode=${role.roleCode}, data-rolename=${role.roleName}"
                                    onclick="editRole(this)">
                                <i class="ri-edit-line"></i> 编辑
                            </button>
                            <button class="btn-small btn-delete" 
                                    th:attr="data-id=${role.id}, data-rolecode=${role.roleCode}, data-rolename=${role.roleName}"
                                    onclick="deleteRole(this)">
                                <i class="ri-delete-bin-line"></i> 删除
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <!-- 添加角色模态框 -->
    <div id="addRoleModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">添加新角色</div>
            <form action="/admin/roles/add" method="post">
                <div class="form-group">
                    <label>角色代码 *</label>
                    <input type="text" name="roleCode" required placeholder="例如: 3, 4, custom">
                </div>
                <div class="form-group">
                    <label>角色名称 *</label>
                    <input type="text" name="roleName" required placeholder="例如: 助教, 评委">
                </div>
                <div class="modal-actions">
                    <button type="button" class="btn-cancel" onclick="closeAddRoleModal()">取消</button>
                    <button type="submit" class="btn-confirm">添加</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 编辑角色模态框 -->
    <div id="editRoleModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">编辑角色</div>
            <form id="editRoleForm" method="post">
                <input type="hidden" id="editRoleId">
                <div class="form-group">
                    <label>角色代码</label>
                    <input type="text" id="editRoleCode" readonly style="background: #f0f0f0;">
                </div>
                <div class="form-group">
                    <label>角色名称 *</label>
                    <input type="text" id="editRoleName" name="roleName" required>
                </div>
                <div class="modal-actions">
                    <button type="button" class="btn-cancel" onclick="closeEditRoleModal()">取消</button>
                    <button type="submit" class="btn-confirm">保存</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 管理权限模态框 -->
    <div id="permissionModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">管理角色权限：<span id="permRoleName"></span></div>
            
            <h4 style="margin: 20px 0 10px; color: #667eea;">当前权限</h4>
            <div id="currentPermissionsList" class="permissions-list">
                <!-- 当前权限将通过JavaScript动态加载 -->
            </div>
            
            <h4 style="margin: 20px 0 10px; color: #667eea;">添加新权限</h4>
            <div class="form-group">
                <select id="newPermissionSelect">
                    <option value="">选择要添加的权限</option>
                    <option th:each="perm : ${allPermissions}" 
                            th:value="${perm.id}" 
                            th:text="${perm.permName + ' (' + perm.permCode + ')'}">权限</option>
                </select>
            </div>
            
            <div class="modal-actions">
                <button type="button" class="btn-cancel" onclick="closePermissionModal()">关闭</button>
                <button type="button" class="btn-confirm" onclick="addPermission()">添加权限</button>
            </div>
        </div>
    </div>

    <script>
        let currentRoleId = null;
        let currentRolePerms = [];

        function showAddRoleModal() {
            document.getElementById('addRoleModal').classList.add('show');
        }
        
        function closeAddRoleModal() {
            document.getElementById('addRoleModal').classList.remove('show');
        }
        
        function editRole(btn) {
            const id = btn.getAttribute('data-id');
            const roleCode = btn.getAttribute('data-rolecode');
            const roleName = btn.getAttribute('data-rolename');
            
            document.getElementById('editRoleId').value = id;
            document.getElementById('editRoleCode').value = roleCode;
            document.getElementById('editRoleName').value = roleName;
            document.getElementById('editRoleForm').action = '/admin/roles/edit/' + id;
            document.getElementById('editRoleModal').classList.add('show');
        }
        
        function closeEditRoleModal() {
            document.getElementById('editRoleModal').classList.remove('show');
        }
        
        function deleteRole(btn) {
            const id = btn.getAttribute('data-id');
            const roleCode = btn.getAttribute('data-rolecode');
            const roleName = btn.getAttribute('data-rolename');
            
            if (roleCode === '0' || roleCode === '1' || roleCode === '2') {
                alert('不能删除系统内置角色！');
                return;
            }
            
            if (confirm('确定要删除角色 "' + roleName + '" 吗？\n\n此操作不可撤销！')) {
                const form = document.createElement('form');
                form.method = 'POST';
                form.action = '/admin/roles/delete/' + id;
                document.body.appendChild(form);
                form.submit();
            }
        }
        
        function manageRolePermissions(btn) {
            currentRoleId = btn.getAttribute('data-id');
            const roleName = btn.getAttribute('data-rolename');
            
            document.getElementById('permRoleName').textContent = roleName;
            
            // 从表格行中获取当前权限
            const row = btn.closest('tr');
            const permBadges = row.querySelectorAll('.perm-badge');
            const permsList = document.getElementById('currentPermissionsList');
            
            if (permBadges.length === 0) {
                permsList.innerHTML = '<div style="color: #999;">暂无权限</div>';
            } else {
                permsList.innerHTML = '';
                permBadges.forEach(badge => {
                    const permName = badge.textContent;
                    const permId = badge.getAttribute('data-perm-id') || '';
                    
                    const item = document.createElement('span');
                    item.className = 'perm-item';
                    item.innerHTML = `${permName} <i class="ri-close-line" onclick="removePermissionByName('${permName}')"></i>`;
                    permsList.appendChild(item);
                });
            }
            
            document.getElementById('permissionModal').classList.add('show');
        }
        
        function closePermissionModal() {
            document.getElementById('permissionModal').classList.remove('show');
        }
        
        function addPermission() {
            const permId = document.getElementById('newPermissionSelect').value;
            
            if (!permId) {
                alert('请选择要添加的权限！');
                return;
            }
            
            const form = document.createElement('form');
            form.method = 'POST';
            form.action = '/admin/roles/add-permission/' + currentRoleId;
            
            const input = document.createElement('input');
            input.type = 'hidden';
            input.name = 'permissionId';
            input.value = permId;
            form.appendChild(input);
            
            document.body.appendChild(form);
            form.submit();
        }
        
        function removePermissionByName(permName) {
            if (confirm('确定要移除权限 "' + permName + '" 吗？')) {
                // 查找权限ID
                const select = document.getElementById('newPermissionSelect');
                let permId = null;
                for (let option of select.options) {
                    if (option.text.startsWith(permName)) {
                        permId = option.value;
                        break;
                    }
                }
                
                if (permId) {
                    const form = document.createElement('form');
                    form.method = 'POST';
                    form.action = '/admin/roles/remove-permission/' + currentRoleId;
                    
                    const input = document.createElement('input');
                    input.type = 'hidden';
                    input.name = 'permissionId';
                    input.value = permId;
                    form.appendChild(input);
                    
                    document.body.appendChild(form);
                    form.submit();
                }
            }
        }
        
        window.onclick = function(event) {
            if (event.target.classList.contains('modal')) {
                closeAddRoleModal();
                closeEditRoleModal();
                closePermissionModal();
            }
        }
        
        // 自动隐藏提示
        setTimeout(() => {
            document.querySelectorAll('.alert').forEach(alert => {
                alert.style.display = 'none';
            });
        }, 5000);
    </script>
</body>
</html>
